#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <conio.h>
#include <windows.h>

#define mas_size 4

double equation(double x);
double poh_ehuaion(double x);
void clrscr();
typedef enum {false=0,true=1} logic;
logic debug_f(char ch);
double newton(double x);
double half(double x,double x2);
double chords(double x1,double x2);
logic cont_f(char co);
void output(double x, double E, unsigned int iter,double time);
HANDLE hconsole;
WORD color1 = (12&0x0f);
WORD color2 = 15 & 0x0f;
struct timer {int hours;
int minutes;
int seconds;
int miliseconds};
struct timer time1;
int main(int argc, char *argv[])
{
   // printf("%d",mas_size);
    hconsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hconsole,color2);
    double x1,x2,x,eps,t1,t2,xi,E,t,xt;
    int num,n1=1,iter=1,nr,cout1=0,count2=0,E1,E2;
    double xc[mas_size];
    char choise,choise2,debug,contin;
    logic cont = true,debug1,cycle=true,inter=true;
   // printf("%d",argc);
    if (argc==1)
    {
        printf("Enter x1,x2: ");
        scanf("%lf%lf",&x1,&x2);
        printf("\nEnter error: ");
        scanf("%lf",&eps);
        printf("%lf",eps);
        if (eps<10e-5 || eps>10e-3)
        {
            printf("Wrong data");
            getch();
            clrscr();
            return main(1,argv[0]);
        }
        printf("\nEnter number of iterations: ");
        scanf("%d",&num);
    }
   // printf("\n%d\n",n);
    if (argc == 5)
    {
        //printf("%s %s %s",argv[1],argv[2],argv[3]);
        x1=atof(argv[1]);
        x2=atof(argv[2]);
        eps=atof(argv[3]);
        if (eps<10e-5 || eps>10e-3)
        {
            printf("Wrong data");
            getch();
            clrscr();
            return main(1,argv[0]);
        }
        num=atoi(argv[4]);
    }
    printf("\nAre you want to debug? ");
        scanf("%s",&debug);
        debug1 = debug_f(debug);
   // clrscr();
    printf("Change method: \n1.Chords (c)\n2.Half-division (h)\n3.Newton's method (n) \n-> ");
    nr=num;
    scanf("%s",&choise);
    if (choise != 'n' && choise != 'N' &&choise != 'c' &&choise != 'C' &&choise != 'h' &&choise != 'H')
    {
        do
        {
            scanf("%s",&choise);
        } while (choise == 'n' && choise == 'N' &&choise == 'c' &&choise == 'C' &&choise == 'h' &&choise == 'H');
    }
    switch (choise)
    {

    case 'n':
    case 'N':
        {
            t1=clock();
            x=x1;
            E=fabs(equation(x));


            while (E>eps&&cont==true)
            {
                n1++;
                iter++;
               xi=newton(x);
                x=xi;
                if (debug1)
                {
                    t2=clock();
                    t=t2-t1;
                   output(x,E,iter,t);
                    getch();
                }
                if (n1>nr&&inter==true)
                {
                    printf("Exceeded the number of iterations. Are you want to continue? ");
                    scanf("%s",&contin);
                    cont = cont_f(contin);
                    n1=0;
                    inter=false;

                }
                E=fabs(equation(x));
                xt=ceil(x);
                E1=fabs(equation(xt));
                }

                t2=clock();
                t=t2-t1;


                output(x,E,iter,t);
            }break;
    case 'c':
    case 'C':
        {
            t1=clock();
            x=x1;
            E=fabs(equation(x));
            while (E>eps&&cont==true)
            {
                n1++;
                iter++;
                xi=chords(x1,x2);
                if (equation(x1)*equation(xi)<0)
                {
                    x2=xi;
                    x=xi;
                }
                else
                {
                    x1=xi;
                    x=xi;
                }
                if (debug1)
                {
                    t2=clock();
                    t=t2-t1;
                   output(x,E,iter,t);
                    getch();
                }
                if (n1>nr&&inter==true)
                {
                     inter=false;
                     printf("Exceeded the number of iterations. Are you want to continue? ");
                    scanf("%s",&contin);
                    cont = cont_f(contin);
                    n1=0;
                    inter=false;
                }
                E=fabs(equation(x));
            }
            t2 = clock();
            t=t2-t1;
            output(x,E,iter,t);
            getch();
        }break;
    case 'h':
    case 'H':
        {
          t1=clock();
          x=x1;
          E=fabs(equation(x));
          while (E>eps&&cont==true)
          {
              n1++;
              iter++;
              xi=half(x1,x2);
              if (equation(x1)*equation(xi)<0)
              {
                  x2=xi;
                  x=xi;
              } else
              {
                  x1=xi;
                  x=xi;
              }
              if (debug1)
              {
                  t2=clock();
                    t=t2-t1;
                   output(x,E,iter,t);
                    getch();
              }
              if (n1>nr&&inter==true)
              {
                  printf("Exceeded the number of iterations. Are you want to continue? ");
                    scanf("%s",&contin);
                    cont = cont_f(contin);
                    n1=0;
                    inter=false;
              }
              E=fabs(equation(x));
          }
          t2=clock();
          t=t2-t1;
          output(x,E,iter,t);
        }break;
        }
    }

void clrscr()
{
    system("cls");
}

double equation(double x)
{
    double y;
    y=pow(x,4)-pow(x,3)-37*pow(x,2)-47*x+84;
    return y;
}

double poh_ehuaion(double x)
{
    double y;
    y = 4*pow(x,3)-3*(x,2)-74*x-47;
    return y;
}

double newton(double x)
{
    double xi,x1;
    x1=x;
    xi=x1-equation(x1)/poh_ehuaion(x1);
    return xi;
}

logic debug_f(char ch)
{
    logic a;
    switch (ch)
    {
    case 'y':
    case 'Y':
        {
            a = true;
        }break;
    case 'n':
    case 'N':
        {
            a=false;
        }break;
        default: a=false;
    }
    return a;
}

logic cont_f(char co)
{
    logic a;
    switch (co)
    {
    case 'y':
    case 'Y':
        {
            a = true;
        }break;
    case 'n':
    case 'N':
        {
            a=false;
        }break;
        default: a=false;
    }
    return a;
}

void output(double x,double E, unsigned int iter,double time)
{
    printf("Root: ");
    SetConsoleTextAttribute(hconsole,color1);
    printf("%lf",x);
    SetConsoleTextAttribute(hconsole,color2);
    printf(" Function: ");
    SetConsoleTextAttribute(hconsole,color1);
    printf("%lf",E);
    SetConsoleTextAttribute(hconsole,color2);
    printf(" Iterations: ", iter);
    SetConsoleTextAttribute(hconsole,color1);
    printf("%u",iter);
    SetConsoleTextAttribute(hconsole,color2);
    printf(" Time: %lf\n",time);
    SetConsoleTextAttribute(hconsole,color1);
    printf("%lf",time);
    SetConsoleTextAttribute(hconsole,color2);
}

double chords(double x1,double x2)
{
    double xi;
    xi = (equation(x2)*x1-equation(x1)*x2)/(equation(x2)-equation(x1));
    return xi;
}

double half(double x1,double x2)
{
    double xi;
    xi=(x1+x2)/2;
    return xi;
}
